package com.situ.web.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.web.entity.Course;
import com.situ.web.entity.PageInfo;
import com.situ.web.entity.Teacher;
import com.situ.web.service.CourseService;

/**
 * Servlet implementation class CourseServlet
 */
@WebServlet("/course")
public class CourseServlet extends HttpServlet {
	private CourseService courseService = new CourseService();
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		String method = req.getParameter("method");
		if(method == null || " ".equals(method)) {
			method = "findByPage";
		}
		switch (method) {
		case "findByPage":
			findByPage(req, resp);
			break;
		case "findAll":
			findAll(req, resp);
			break;
		case "deleteById":
			deleteById(req, resp);
			break;
		case "insert":
			insert(req, resp);
			break;
		case "selectById":
			selectById(req, resp);
			break;
		case "update":
			update(req, resp);
			break;

		default:
			break;
		}
	}
	private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String name = req.getParameter("name");
		String credit = req.getParameter("credit");

		Course course = new Course(Integer.parseInt(id), name,  Integer.parseInt(credit));
		System.out.println(course);
		
		boolean isSuccess = courseService.update(course);
		
		if (isSuccess) {
			// 修改完之后，重定向到/Java1912Web/findAllStudent
			resp.sendRedirect(req.getContextPath() + "/course?method=findByPage");
		} else {
			resp.sendRedirect(req.getContextPath() + "/error.html");
		}
		
	}
	private void selectById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String selectId = req.getParameter("id");
	    Course course = courseService.selectById(Integer.parseInt(selectId));
		req.setAttribute("course", course);
		req.getRequestDispatcher("/course_update.jsp").forward(req, resp);  
		
	}
	private void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String name = req.getParameter("name");
		String credit = req.getParameter("credit");
		System.out.println(name);
		System.out.println(credit);
		Course course = new Course(name, Integer.parseInt(credit));
		
		boolean isSuccess = courseService.insert(course);
		
		// 添加完之后再到查询并展示所有的界面
		// http://localhost:8080/Java1912Web/findAllStudent
		resp.sendRedirect(req.getContextPath() + "/course?method=findByPage");
		
	}
	private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		System.out.println("id:" + id);
		boolean isSuccess = courseService.deleteById(Integer.parseInt(id));
		resp.sendRedirect(req.getContextPath() + "/course?method=findAll");
		
	}
	private void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<Course> list = courseService.findAll();
		for (Course course : list) {
			System.out.println(course);
		}
		req.setAttribute("list", list);
		req.getRequestDispatcher("/course_list.jsp").forward(req, resp);
		
	}
	private void findByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String pageNo = req.getParameter("pageNo");
		String pageSize = req.getParameter("pageSize");
		// 如果pageNo和pageSize没有传递，设置去pageNo=1,pageSize=5
		if (pageNo == null || "".equals(pageNo)) {
			pageNo = "1";
		}
		if (pageSize == null || "".equals(pageSize)) {
			pageSize = "5";
		}
		//传给service
		PageInfo pageInfo = courseService.findByPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
		req.setAttribute("pageInfo",pageInfo);
		req.getRequestDispatcher("/course_page_list.jsp").forward(req, resp);
		
	}

}
